<?php
/**
* 提现记录
*/
class Withdraw_model extends CI_Model
{
	
	private $_table  = 'withdraw';
	private $_table1 = 'withdraw_audit';

	/**
	 * 总页数
	 */
	public function total($params)
	{
        $params = array_map('trimArray', $params);
		$this->db->select('*');
		$this->db->from($this->_table);
        if (!empty($params['transfer_id'])) {
            $this->db->where_in("withdraw.transfer_id", $params['transfer_id']);
        }
        if (!empty($params['status'])) {
            $this->db->where_in("withdraw.status", $params['status']);
        }
        if ( ! empty($params['audit_status'])) {
            $this->db->where("withdraw.audit_status", $params['audit_status']);
        }
        if (!empty($params['amount'])) {
            $this->db->where("withdraw.amount", $params['amount']);
        }
        if (!empty($params['withdraw_id'])) {
            $this->db->where("withdraw.withdraw_id", trim($params['withdraw_id']));
        }

        if ( ! empty($params['scenery_name'])) {
            $this->db->where("((`scenery_name` LIKE '%{$params['scenery_name']}%') OR (`sid`='{$params['scenery_name']}'))");
        }

        if ( ! empty($params['user_name'])) {
            $this->db->where("((`user_name` LIKE '%{$params['user_name']}%') OR (`uid`='{$params['user_name']}'))");
        }

        if ( ! empty($params['mobile_phone'])) {
            $this->db->like("mobile_phone", $params['mobile_phone']);
        }

        if ( ! empty($params['start_date'])) {
            $this->db->where("created_at >=", $params['start_date']. ' 00:00:01');
        }
        if ( ! empty($params['end_date'])) {
            $this->db->where("created_at <=", $params['end_date']. ' 23:59:59');
        }
        if ( ! empty($params['cardholder'])) {
            $this->db->like("cardholder", $params['cardholder']);
        }
        if ( ! empty($params['is_from'])) {
            $this->db->where_in('withdraw.is_from', $params['is_from']);
        }

        return $this->db->count_all_results();
	}

	/**
	 * 查询提现记录
	 */
	public function page_list($params, $page_num, $num)
	{
        $params = array_map('trimArray', $params);
		$this->db->select('withdraw.withdraw_id, transfer_id, withdraw.uid, sid, scenery_name, user_name, amount, amount_carry, is_from, status, withdraw.state, withdraw.created_at, withdraw.updated_at, bank_id, bank_card, cardholder, pin, bank_name, mobile_phone, audit_status, transfer_amount, poundage');
		$this->db->from($this->_table);
        if (!empty($params['transfer_id'])) {
            $this->db->where_in("withdraw.transfer_id", $params['transfer_id']);
        }
        if (!empty($params['status'])) {
        	$this->db->where_in("withdraw.status", $params['status']);
        }
        if ( ! empty($params['audit_status'])) {
            $this->db->where("withdraw.audit_status", $params['audit_status']);
        }
        if (!empty($params['amount'])) {
            $this->db->where("withdraw.amount", $params['amount']);
        }
        if (!empty($params['withdraw_id'])) {
            $this->db->where("withdraw.withdraw_id", trim($params['withdraw_id']));
        }

        if ( ! empty($params['scenery_name'])) {
            $this->db->where("((`scenery_name` LIKE '%{$params['scenery_name']}%') OR (`sid`='{$params['scenery_name']}'))");
        }

        if ( ! empty($params['user_name'])) {
            $this->db->where("((`user_name` LIKE '%{$params['user_name']}%') OR (`uid`='{$params['user_name']}'))");
        }

        if ( ! empty($params['mobile_phone'])) {
            $this->db->like("mobile_phone", $params['mobile_phone']);
        }

        if ( ! empty($params['start_date'])) {
            $this->db->where("created_at >=", $params['start_date']. ' 00:00:01');
        }
        if ( ! empty($params['end_date'])) {
            $this->db->where("created_at <=", $params['end_date']. ' 23:59:59');
        }
        if ( ! empty($params['cardholder'])) {
            $this->db->like("cardholder", $params['cardholder']);
        }

        if ( ! empty($params['is_from'])) {
            $this->db->where_in('withdraw.is_from', $params['is_from']);
        }

        $this->db->order_by('withdraw.withdraw_id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
	}

	/**
	 * 根据主键查找
	 */
	public function findById($id)
	{
		$this->db->select('withdraw_id, transfer_id, withdraw.uid, sid, scenery_name, user_name, amount, transfer_amount, poundage, bank_id, pay_time,bank_name, amount_carry, is_from, status, state, withdraw.created_at, withdraw.updated_at, bank_card, cardholder, pin, mobile_phone, audit_status, transfer_status');
		$this->db->from($this->_table);
		$this->db->where('withdraw_id', (int)$id);
		$this->db->limit(1);
		return $this->db->get();
	}

	/**
	 * 更新审核状态
	 */
	public function updateState($withdrawId, $data)
	{
		$this->db->where('withdraw_id', (int)$withdrawId);
		return $this->db->update($this->_table, $data);
	}

    /**
     * 添加
     */
    public function insertWithdraw($postData)
    {
        $data = array(
            'transfer_id'      => $postData['transfer_id'], //自动转账编号 默认为0
            'sid'              => isset($postData['sid']) ? $postData['sid'] : 0,
            'scenery_name'     => isset($postData['scenery_name']) ? $postData['scenery_name'] : '',
            'uid'              => $postData['uid'],
            'user_name'        => isset($postData['user_name']) ? $postData['user_name'] : '',
            'amount'           => $postData['amount'],
            'amount_carry'     => isset($postData['amount_carry']) ? $postData['amount_carry'] : 0,
            'transfer_amount'  => isset($postData['transfer_amount']) ? $postData['transfer_amount'] : 0,
            'poundage'         => isset($postData['poundage']) ? $postData['poundage'] : 0,
            'bank_id'          => isset($postData['bank_id']) ? $postData['bank_id'] : 0,
            'bank_name'        => isset($postData['bank_name']) ? $postData['bank_name'] : '对方未提供银行卡号',
            'bank_card'        => isset($postData['bank_card']) ? $postData['bank_card'] : '',
            'cardholder'       => isset($postData['cardholder']) ? $postData['cardholder'] : '',
            'pin'              => isset($postData['pin']) ? $postData['pin'] : '',
            'mobile_phone'     => isset($postData['mobile_phone']) ? $postData['mobile_phone'] : 0,
            'is_from'          => 3, // 3 需要自动打款的
            'status'           => 2, // 确认
            'state'            => 0, //待一审
            'audit_status'     => 0, //待审核
            'transfer_status'  => 1, //待交易
            'remark'           => '', //错误消息
            'created_at'       => date('Y-m-d H:i:s'),
            'pay_time'         => isset($postData['pay_time']) ? $postData['pay_time'] : date('Y-m-d H:i:s'),
            'updated_at'       => '0000-00-00 00:00:00'
            );
        $this->db->insert($this->_table, $data);
        return $this->db->insert_id();
    }

    /**
     * 查找最后一条    景区扫码的记录
     */
    public function lastWithdraw()
    {
        $this->db->select('withdraw_id, created_at');
        $this->db->from($this->_table);
        $this->db->where('is_from', 3); //景区扫码
        $this->db->limit(1);
        $this->db->order_by('withdraw_id', 'DESC');
        return $this->db->get();
    }

    /**
     * 更新银行卡信息
     */
    public function updateBank($id, $bankInfo)
    {
        $data = array(
            'uid'              => $bankInfo->uid,
            'user_name'        => $bankInfo->user_name,
            'bank_id'          => $bankInfo->bank_id,
            'bank_name'        => $bankInfo->bank_name,
            'bank_card'        => $bankInfo->bank_card,
            'cardholder'       => $bankInfo->cardholder,
            'pin'              => $bankInfo->pin,
            'mobile_phone'     => $bankInfo->mobile_phone,
            'updated_at'       => date('Y-m-d H:i:s')
            );
        $this->db->where('withdraw_id', $id);
        return $this->db->update($this->_table, $data);
    }

    /**
     * 根据交易编码  查找要处理的交易
     */
    public function findByTransferSn($transferId)
    {
        $this->db->select('withdraw_id, transfer_id, withdraw.uid, sid, scenery_name, user_name, amount, bank_id, pay_time,bank_name, amount_carry, is_from, status, state, withdraw.created_at, withdraw.updated_at, bank_card, cardholder, pin, mobile_phone, audit_status');
        $this->db->from($this->_table);
        $this->db->where('transfer_id', $transferId);
        $this->db->where('state', 2); //二审
        $this->db->where('audit_status', 2); //审核通过
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 根据交易编码  查找要处理的交易
     */
    public function excelExport($transferId)
    {
        $this->db->select('withdraw_id, bank_card, cardholder, amount, pay_time, uid, scenery_name, bank_name, amount_carry, transfer_amount, poundage');
        $this->db->from($this->_table);
        $this->db->where('transfer_id', $transferId);
        $this->db->where('state', 2); //二审
        $this->db->where('audit_status', 1); //审核通过
        $this->db->order_by('withdraw_id', 'ASC');
        return $this->db->get();
    }

    /**
     * 批量修改提现转账状态
     */
    public function updateBatch($data)
    {
        $this->db->update_batch($this->_table, $data, 'withdraw_id');
    }
}